let app = getApp();
let API_URL = app.globalData.API_URL;
import { getVideoById } from "../../../../request/api";

Page({

    /**
     * 页面的初始数据
     */
    data: {
        waiting: true,
        current: {},
        videoList: [],
        changeIndex: 0,
        lestTime: 0, // 历时

        // 分页
        pageNo: 1,
        totalPage: 0,

        // 状态栏高度
        statusBarHeight: wx.getStorageSync('statusBarHeight') + 'px',

        // 导航栏高度
        navigationBarHeight: wx.getStorageSync('navigationBarHeight') + 'px',

        // 胶囊按钮高度
        menuButtonHeight: wx.getStorageSync('menuButtonHeight') + 'px',

        // 导航栏和状态栏高度
        navigationBarAndStatusBarHeight:
            wx.getStorageSync('statusBarHeight') +
            wx.getStorageSync('navigationBarHeight') +
            'px',
    },

    // 获取内容
    getPost(index) {
        let status = -1
        let userId = this.data.current.userId
        let data = { status, userId, pageNo: this.data.pageNo }
        getVideoById(res => { // get videos
            if (res.statusCode == 200) {
                if (res.data && typeof res.data.data != "undefined" && res.data.data.length > 0) {
                    let videosInCurrPage = res.data.data
                    for (let item of videosInCurrPage) {
                        item["pageNo"] = this.data.pageNo
                    }
                }
                this.setData({ videoList: this.data.videoList.concat(res.data.data), totalPage: res.data.totalPage })
                if (this.data.videoList.length > 0) this.setData({ videoUrl: this.data.videoList[index].url })
            }
        }, data);
    },

    // 返回
    previous() {
        wx.navigateBack({
            delta: 1,
            fail: err => {
                console.log("err: ", err);
                wx.navigateTo({
                    url: '/pages/index/index',
                })
            }
        })
    },
    waitingToPlay(e) {
        // console.log("waitingToPlay: ", e);
    },

    // 当视频数据加载完时
    videoLoaded(e) {
        // console.log("videoLoaded: ", e);
        this.setData({ waiting: false })
    },

    // 
    loadVideOnError(e) {
        // console.log("loadVideOnError: ", e);
        this.setData({ waiting: false })
    },

    //划动切换
    slide(e) {
        this.setData({ waiting: true })
        let currentIndex = e.detail.current
        let videoItem = this.data.videoList[currentIndex]
        let listLen = this.data.videoList.length

        // console.log("this.data.pageNo: ", this.data.pageNo);
        // console.log("currentIndex: ", currentIndex);
        // console.log("videoItem: ", videoItem);
        // console.log("list length: ", listLen);

        if (listLen - 1 == currentIndex) {
            // console.log("swpied to the last item: ", videoItem);
            if (this.data.pageNo == this.data.totalPage) {
                this.setData({ 
                    videoUrl : videoItem.url,
                    changeIndex : currentIndex
                })
                return
            }
            
            this.data.pageNo += 1
            this.setData({ pageNo: this.data.pageNo })
            this.getPost(currentIndex)
        }
        // console.log("ohho!");
        this.setData({ 
            videoUrl : videoItem.url,
            changeIndex : currentIndex
        })
    },

    // 点击喜欢
    onClickLike(e) {
        // 获取当前时间
        let currentTime = new Date().valueOf();

        // 首次请求
        if (this.data.lestTime == 0) {
            this.setData({ lestTime: currentTime });
            this.likeFun(e);
        }

        // 次请求
        if (currentTime - this.data.lestTime > 3000) {
            this.setData({ lestTime: currentTime })
            this.likeFun(e);
        }
    },

    // 检查登录状态后调用喜欢方法
    likeFun(e){
        let promise = new Promise(function (resolve, rej) {
            app.login(res => {
                resolve(res)
            })
        })

        promise.then(
            resolve => {
                let videoId = e.currentTarget.dataset.item.id
                let item = this.data.videoList[e.currentTarget.dataset.index]
                item.isLike = !item.isLike
                item.favorite_count = item.favorite_count + (item.isLike ? 1 : -1)
                this.setData({ videoList: this.data.videoList })
                let data = { videoId }

                wx.request({
                    url: `${API_URL}/videoFavorite`,
                    header: app.header(),
                    data: data,
                    method: 'post',
                    success: function (res) {
                        if (res.statusCode == 200) {

                        }
                    }
                })
            }
        )
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let value = JSON.parse(options.json);
        // console.log("opts: ", value);
        let index = value.index - (value.pageNo - 1) * 9
        this.setData({ current: value, changeIndex: index, pageNo: value.pageNo, });
        this.getPost(index);
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})